package com.lbn.jfqsc.app.account.info;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.lbn.baseframe.mvp.BasePresenter;
import com.lbn.baseframe.net.business.RxSubscribe;
import com.lbn.baseframe.utils.L;
import com.lbn.jfqsc.App;
import com.lbn.jfqsc.business.bean.UpToken;
import com.lbn.jfqsc.business.bean.User;
import com.lbn.jfqsc.net.Host;
import com.lbn.jfqsc.widget.UploadFactory;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 李炳南
 * @date 2017/3/1
 */
public class InfoPresenter extends BasePresenter<InfoView> {

    private int id;
    private String name;
    private String email;


    public void upToken(final File file,String name,String email){

        if( isEmpty(name,email)){
            return;
        }

        this.name = name;
        this.email = email;

        getView().showLoading();

        subscribeNetwork(Host.getApi().upToken(), new RxSubscribe<UpToken>() {
            @Override
            public void next(UpToken o) {

                if(o!=null&&o.getUpToken()!=null){
                    upLoadImage(file,o.getUpToken());
                }else{
                    getView().onError("网络异常");
                    getView().hideLoading();
                }
            }

            @Override
            public void error(Throwable e) {
                getView().hideLoading();
                getView().onError(e.getMessage());
            }
        });
    }



    public void upLoadImage(File file,String token){

        File data = file;
        final String dataName = App.getUser().getPhone()+"_"+System.currentTimeMillis()+"_"+data.getName();
        UploadFactory.instance().uploadManager().put(data, dataName, token,
                new UpCompletionHandler() {
                    @Override
                    public void complete(String key, ResponseInfo info, JSONObject res) {
                        //res包含hash、key等信息，具体字段取决于上传策略的设置
                        getView().hideLoading();
                        if(info.isOK())
                        {
                            upUserInfo(name,email,dataName);
//                            Log.i("qiniu", "Upload Success");
                        }
                        else{
//                            Log.i("qiniu", "Upload Fail");
                            //如果失败，这里可以把info信息上报自己的服务器，便于后面分析上传错误原因
                            getView().onError("无法连接图片服务器");
                        }
                        L.e("qiniu", key + ",\r\n " + info + ",\r\n " + res);
                    }
                }, null);
    }


    public void upUserInfo(String name,String email,String dataName){

        if( isEmpty(name,email)){
            return;
        }

        getView().showLoading();

        Map<String,String>map = new HashMap<>();
        map.put("id",App.getUser().getId()+"");
        map.put("name",name);
        map.put("email",email);
        if(dataName!=null){
            map.put("head",dataName);
        }
//        map.put("gender",id);
//        map.put("id",id);
        subscribeNetwork(Host.getApi().upInfo(map), new RxSubscribe() {
            @Override
            public void next(Object o) {
                getView().hideLoading();
                getView().onSucceed();
            }

            @Override
            public void error(Throwable e) {
                getView().hideLoading();
                getView().onError(e.getMessage());
            }
        });
    }


    public boolean isEmpty(String...strings){
        for(String s: strings){
            if(TextUtils.isEmpty(s.trim())){
                getView().onError("信息不能为空");
                return true;
            }
        }
        return false;
    }




    public void login(){

        getView().showLoading();

        Map<String,String>map = new HashMap<>();
        map.put("phone",App.getUser().getPhone());
        map.put("password",App.getUser().getPassword());

        subscribeNetwork(Host.getApi().token(map), new RxSubscribe<User>() {
            @Override
            public void next(User user) {
                getView().hideLoading();
                L.e("UserJson:"+new Gson().toJson(user));
                user.setPassword(App.getUser().getPassword());
                getView().onSucceed(user);
            }

            @Override
            public void error(Throwable e) {
                getView().hideLoading();
                getView().onError(e.getMessage());
            }
        });
    }


}
